#include <iostream>

#include "SKBookList.hpp"


typename SKBookList::iterator SKBookList::begin()
{
    return iterator(this, front_);
}

typename SKBookList::iterator SKBookList::end()
{
    return iterator(this, 0);
}

typename SKBookList::SKBookListNode* SKBookList::createNode(SKBookNode* node)
{
    return new SKBookListNode(node);
}

void SKBookList::addNode(SKBookNode* node)
{
    ++size_;
    if (!front_)
    {
	front_ = createNode(node);
	return;
    }

    SKBookListNode* traverseNode = front_;

    while(traverseNode->next_)
    {
	traverseNode = traverseNode->next_;
    }

    traverseNode->next_ = createNode(node);
    return;
}

bool SKBookList::deleteNode(SKBookNode* node)
{
    SKBookListNode* current = front_;
    SKBookListNode* previous = NULL;

    while(current)
    {
	if (current->node_ == node)
	{
	    if (previous)
	    {
		previous->next_ = current->next_;
		delete current;
		current = previous->next_;
	    }
	    else
	    {
		// no previous .. so update front_
		front_ = front_->next_;
		delete current;
		current = front_;
	    }
	    --size_;
	    return true;
	}
	previous = current;
	current = current->next_;
    }

    return false;
}
	  
void SKBookList::clear()
{
    while(front_)
    {
	SKBookListNode* tmp = front_;
	front_ = front_->next_;
	delete tmp;
    }
    front_ = 0;
}
